/**
 * Autoplay vids
 */
var autoplay = {
	
	nowPlaying : -1,
	fadingOrigin : -1,
	fadingFrom : 0,
	fadingTo : 0,
	fadingStarted : 0,
	fading : false,
	enabled : false,
	virgin : true,
	
	enable : function() {
		debug.print("autoplay.enable");
		this.enabled = true;
		this.setState();
		this.startPlaying();
	},
	
	startPlaying : function() {
		debug.print("autoplay.startPlaying");
		// play or load video
		if (this.nowPlaying === -1) {
			if (player[0].ready === true) {
				controller.togglePlay(0);
				this.nowPlaying = 0;
			} else if (player[1].ready === true) {
				controller.togglePlay(1);
				this.nowPlaying = 1;
			} else if (np.count() > 0) {
				this.nowPlaying = 0;
				player[0].loadAndPlay(np.getNext().data("video"));
				np.remove(np.getNext());
			}
		}
		if (this.virgin === true && this.nowPlaying != -1) {
			var value = this.nowPlaying * 100;
			$("#fader-container").slider('option', 'value', value);
			controller.faderChange();
		}
		this.virgin = false;
	},
	
	disable : function() {
		debug.print("autoplay.disable");
		this.fadeStop();
		this.enabled = false;
	},
	
	isEnabled : function() {
		debug.print("autoplay.isEnabled");
		return this.enabled;
	},
	
	setState : function() {
		debug.print("autoplay.setState");
		// don't update the state while fading
		if (this.fading === true) {
			return false;
		}
		this.nowPlaying = -1;
		// get active player
		if (player[0].state == 1 && player[1].state != 1) { // player 0 is playing
			this.nowPlaying = 0;
		} else if (player[1].state == 1 && player[0].state != 1) { // player 1 is playing
			this.nowPlaying = 1;
		} else if (player[0].state != 1 && player[1].state != 1) { // neither are playing
			this.nowPlaying = -1;
		} else if (player[0].state == 1 && player[1].state == 1) { // both are playing
			// try to detect volume
			if ($("#fader-container").slider('option', 'value') == 0) {
				this.nowPlaying = 0;
			} else if ($("#fader-container").slider('option', 'value') == 100) {
				this.nowPlaying = 1;
			} else {
				// both are playing and both have sound on
				// TODO detect which video is finished first
			}
		}
	},
	
	check : function() {
		debug.print("autoplay.check");
		this.setState();
		if (this.nowPlaying != -1 && this.enabled === true) { // at least one player is playing
			// if no video is currently cued, but there is one in the np
			if ((player[0].ready === false || player[1].ready === false) && np.count() > 0) {
				if (player[0].ready === false) {
					this.loadNext(0);
				} else if (player[1].ready === false) {
					this.loadNext(1);
				}
			}
			// check if playing video is ending
			var remainingTime = player[this.nowPlaying].getDuration() - player[this.nowPlaying].getCurrentTime();
			if (remainingTime < config.fadingAt && this.fading === false) { // end in sight
				this.fadeStart();
			}
			if (this.fading === true) { // fade has begun, continue
				var perc =  parseInt( (player[this.nowPlaying].getCurrentTime() - this.fadingStarted) / config.fadingAt * 100);
				this.fade(perc);
				//this.fade( 100 - parseInt(remainingTime / config.fadingAt * 100) );
			}
		}
	},
	
	fadeStart : function() {
		if (this.fading === true || this.nowPlaying == -1) {
			return false;
		}
		debug.print("autoplay.fadeStart");
		this.fading = true;
		this.fadingOrigin = this.nowPlaying;
		this.fadingStarted = player[this.nowPlaying].getCurrentTime();
		this.fadingFrom = $("#fader-container").slider('option', 'value');
		this.fadingTo = other(this.nowPlaying) * 100;
		player[other(this.nowPlaying)].play();
	},
	
	fadeStop : function() {
		debug.print("autoplay.fadeStop");
		this.fading = false;
	},
	
	fade : function(perc) {
		debug.print("autoplay.fade [perc: " + perc + "]");
		var newVal = ((this.fadingTo - this.fadingFrom) * (perc / 100)) + this.fadingFrom;
		$("#fader-container").slider('option', 'value', newVal);
		if (perc > 100) {
			// fader ended, load next video?
			if (player[this.fadingOrigin].getDuration() - player[this.fadingOrigin].getCurrentTime() > 2) {
				this.loadNext(this.fadingOrigin);
			}
			this.fadeStop();
		}
		controller.faderChange();
	},
	
	skip : function() {
		if (player[other(this.nowPlaying)].ready === true) {
			player[other(this.nowPlaying)].play();
			$("#fader-container").slider('option', 'value', other(this.nowPlaying) == 0 ? 0 : 100);
			controller.faderChange();
			this.loadNext(this.nowPlaying);		
		}
	},
	
	loadNext : function(key) {
		debug.print("autoplay.loadNext [key: " + key + "]");
		if (np.count() > 0) {
			player[key].cue(np.getNext().data("video"));
			np.remove(np.getNext());
		}
	}
	
}
